Repository navigation
gh-161972: Fix cmath.log(0, base) returning (-inf+nanj) instead of raising ValueError - #161986
tanveer-arch wants to merge 3 commits into
Conversation
… of raising ValueError
skirpichev
left a comment
There was a problem hiding this comment.
No, it's a wrong place to fix.
c_log() is the problem. In libc no library call set errno to zero.
|
Thanks, you're right the fix belongs in |
| @@ -0,0 +1 @@ | |||
| Fix :func:`cmath.log` with a nonzero base: ``cmath.log(0, base)`` now raises :exc:`ValueError` instead of returning ``(-inf+nanj)``. | |||
There was a problem hiding this comment.
| Fix :func:`cmath.log` with a nonzero base: ``cmath.log(0, base)`` now raises :exc:`ValueError` instead of returning ``(-inf+nanj)``. | |
| Fix :func:`cmath.log` with a nonzero base: ``cmath.log(0, base)`` now raises | |
| :exc:`ValueError` instead of returning ``(-inf+nanj)``. |
| } | ||
| } | ||
| r.imag = atan2(z.imag, z.real); | ||
| errno = 0; |
There was a problem hiding this comment.
I would prefer to leave c_log() unchanged: c_xxx() functions are documented to set errno to 0 on success, see #161972 (comment).
Instead, check errno after the first c_log() call in cmath_log_impl(). Something like:
{
x = c_log(x);
if (errno != 0) {
return math_error();
}
if (y_obj != NULL) {
Py_complex y = PyComplex_AsCComplex(y_obj);
if (PyErr_Occurred()) {
return NULL;
}
y = c_log(y);
if (errno != 0) {
return math_error();
}
x = _Py_c_quot(x, y);
}
return PyComplex_FromCComplex(x);
}There was a problem hiding this comment.
Something like:
_Py_c_quot() also can set errno.
| _testcapi.set_errno(0) | ||
| self.check_polar(polar_with_errno_set) | ||
|
|
||
| def test_log_zero_with_base(self): |
There was a problem hiding this comment.
cmath.log(2, 0) doesn't call cmath.log(0, base), so I suggest to rename the test to:
| def test_log_zero_with_base(self): | |
| def test_log_with_base(self): |
I would prefer to add the new test at the end of the CMathTests class.
Ah? But the comment that you cited at #161972 (comment) says that c_xxx() functions set errno on success. Does it mean that the comment is outdated? |
Strictly speaking, it's about module functions. And this is still be valid for cmath_log_impl(). But I think that better would be, if we remove settings of errno to 0 in module functions. The Py_complex_protected_converter already do this. I think AC should do this before any call to |
Fixes #161972.
cmath.log(0)raisesValueError, butcmath.log(0, 2)returned(-inf+nanj). The domain error fromc_log(x)was stored inerrno, and the second callc_log(y)reseterrnoto 0 on success before the check, so the error was lost.The fix saves
errnoafter the firstc_log()call and restores it after the second one, following the same save/restore patterncmath_log10_implalready uses.cmath.log(0, base)now raisesValueErrorfor any base, and a base of 0 or 1 still raises for a nonzero argument. Normal two-argument logs are unchanged (cmath.log(2, 2) == 1+0j).Added a regression test
test_log_zero_with_baseinLib/test/test_cmath.pyand a news entry.I used an AI assistant to help draft this change and reviewed it in detail; I understand the fix and can explain it.